using System;
using System.Collections;

namespace Microsoft.Samples.Windows.Forms.Navigation
{
	/// <summary>
	/// A page the user has visited, along with other information that establishes
	/// the context in which the user visited the page.
	/// </summary>
	/// <remarks>
	/// Instances of this class are stored on a Frame's Back and Forward stacks. There will
	/// be a separate context on these stacks for each time the user visited a page. I.e.,
	/// if the user navigates to a particular page three times, three separate contexts
	/// will be created.
	/// </remarks>
	internal class NavigationContext
	{
		/// <summary>
		/// The page that was visited in this context.
		/// </summary>
		private Page page;

		/// <summary>
		/// Collection of view state fields.
		/// </summary>
		public ViewState ViewState = null;

		/// <summary>
		/// The position of the horizontal scroll bar when the page was visited in this context.
		/// </summary>
		public int HorizontalScrollPosition = 0;

		/// <summary>
		/// The position of the vertical scroll bar when the page was visited in this context.
		/// </summary>
		public int VerticalScrollPosition = 0;

		/// <summary>
		/// Create a new navigation context.
		/// </summary>
		/// <param name="page">The page shown in this context</param>
		public NavigationContext(Page page)
		{
			this.page = page;
		}

		/// <value>
		/// The page visited in this context.
		/// </value>
		public Page Page
		{
			get
			{
				return page;
			}
		}

		/// <summary>
		/// The text of the page as it appeared in this navigation context.
		/// </summary>
		/// <remarks>
		/// This property may different from the page's current Text property.
		/// </remarks>
		public string PageText
		{
			get
			{
				if (page == null)
				{
					return string.Empty;
				}
				else if (ViewState == null)
				{
					// Return the page's current text.
					return page.Text;
				}
				else
				{
					// Return the saved text.
					return (string) ViewState.ReadProperty("Text");
				}
			}
		}
	}
}
